package com.cgsd.heyu.widgets;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import com.cgsd.heyu.R;

/**
 * @author xiaoxing
 * Created by pengsheng-2 on 2018/10/10.
 * 错误页面、空页面
 */

public class ErrorCustomView extends LinearLayout {
    private String[] dotText = {" .    ", " . .  ", " . . ."};
    public static final String EMPTY = "empty";
    public static final String TIME_OUT = "time_out";
    public static final String LOADING = "loading";
    private TextView errorTiTile;
    private TextView errorReload;
    private ImageView errorImage;
    private ValueAnimator valueAnimator;
    private String title;

    public void setReLoadListener(OnClickListener onClickListener) {
        if (errorReload != null) {
            errorReload.setOnClickListener(onClickListener);
        }
    }

    public ErrorCustomView(Context context) {
        this(context, null);
    }

    public ErrorCustomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ErrorCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.layout_error,this);
        errorTiTile = findViewById(R.id.error_title);
        errorReload = findViewById(R.id.error_reload);
        errorImage = findViewById(R.id.error_image);
    }

    /**
     * 页面加载中
     */
    public void showLoading(){
        showType(R.mipmap.default_no_net,"努力加载中",LOADING);
    }

    /**
     * 数据为空
     */
    public void showEmpty() {
        showEmpty(-1);
    }

    /**
     * 数据为空
     *
     * @param drawableResId
     */
    public void showEmpty(@DrawableRes int drawableResId) {
        showEmpty(drawableResId, null);
    }


    /**
     * 数据为空
     *
     * @param drawableResId
     * @param title
     */
    @SuppressLint("ResourceType")
    public void showEmpty(@DrawableRes int drawableResId, String title) {
        if (title == null){
            title = "数据为空";
        }
        if (drawableResId <= 0){
            drawableResId = R.mipmap.default_no_data;
        }
        showType(drawableResId,title,EMPTY);
    }


    /**
     * 网络错误
     */
    public void showNetworkError(){
        showNetworkError(-1);
    }

    /**
     * 网络错误
     * @param drawableResId
     */
    public void showNetworkError(@DrawableRes int drawableResId){
        showNetworkError(drawableResId,null);
    }


    /**
     * 网络错误
     * @param drawableResId
     * @param title
     */
    @SuppressLint("ResourceType")
    public void showNetworkError(@DrawableRes int drawableResId, String title){
        if (title == null){
            title = "网络连接错误";
        }
        if (drawableResId <= 0){
            drawableResId = R.mipmap.default_no_net;
        }
        showType(drawableResId,title,TIME_OUT);
    }


    /**
     * 总设置
     */
    @SuppressLint("ResourceType")
    public void showType(@DrawableRes int drawableResId, String content, String type){
        if (drawableResId <= 0) {
            errorImage.setImageResource(R.mipmap.default_no_data);
        } else {
            errorImage.setImageResource(drawableResId);
        }
        title = content;
        if (content == null){
            title = "未知错误";
        }
        if (LOADING.equals(type)){
            if (valueAnimator == null) {
                valueAnimator = ValueAnimator.ofInt(0, 3).setDuration(2000);
                valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int i = (int) animation.getAnimatedValue();
                        errorTiTile.setText(title + dotText[i % dotText.length]);
                    }
                });
            }
            valueAnimator.start();
        }else {
            if (valueAnimator != null){
                valueAnimator.cancel();
            }
            errorTiTile.clearAnimation();
            errorTiTile.setText(title);
        }
    }
}
